package com.landeb.shop.huo.act;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.manager.RequestTracker;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.landeb.shop.R;
import com.landeb.shop.huo.MyApplication;
import com.landeb.shop.huo.adapter.ActScoreLogRVAdapter;
import com.landeb.shop.huo.adapter.ActShopDetailGvAdapter;
import com.landeb.shop.huo.adapter.ActShopDetailRvAdapter;
import com.landeb.shop.huo.bean.BeanActShopDetail;
import com.landeb.shop.huo.httpRequest.ActShopDetailRequest;
import com.landeb.shop.huo.httpRequest.ActShopListRequset;
import com.landeb.shop.huo.view.FixedGridView;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ShopDetailActivity extends BaseActivity implements View.OnClickListener {

    private String shopId;
    private String shopTEL;

    private LinearLayout btnBack;
    private SwipeRefreshLayout mSRL;
    private ImageView mLogo;
    private LinearLayout llGoodsCount, llFocusCount, llDealCount;
    private TextView tvGoodsCount, tvFocusCount, tvDealCount, tvShopName;
    //    private FixedGridView mGridView;
    private Button btnAllKinds, btnCallService;

    private List<BeanActShopDetail.DataBean.GoodsBean> mList;
    //    private ActShopDetailGvAdapter mAdapter;
    private ActShopDetailRvAdapter mAdapter;

    private String mUrl;
    private Retrofit mRetrofit;
    private ActShopDetailRequest mRequest;
    private boolean isRefreshing = false;

    private RecyclerView mRecyclerView;

    public static void toAct(Context context) {
        Intent intent = new Intent(context, ShopDetailActivity.class);
        context.startActivity(intent);
    }

    public static void toAct(Context context, String shopId) {
        Intent intent = new Intent(context, ShopDetailActivity.class);
        intent.putExtra("shopId", shopId);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_detail);

        Intent intent = getIntent();
        shopId = intent.getStringExtra("shopId");

        OkHttpClient client = MyApplication.getClient();
        mUrl = MyApplication.BASE_URL;
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        mRequest = mRetrofit.create(ActShopDetailRequest.class);


        initView();
        initData();
        initEvent();
        refresh();

    }

    private void refresh() {

        mSRL.setRefreshing(true);

        if (!isRefreshing) {

            isRefreshing = true;

            mRequest.getShopData(shopId)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<BeanActShopDetail>() {
                        @Override
                        public void onCompleted() {
                            Log.e("getShopData", "onCompleted: ");
                            mSRL.setRefreshing(false);
                            isRefreshing = false;
                        }

                        @Override
                        public void onError(Throwable e) {
                            Log.e("getShopData", "onError: " + e.toString());
                            mSRL.setRefreshing(false);
                            isRefreshing = false;
                        }

                        @Override
                        public void onNext(BeanActShopDetail beanActShopDetail) {

                            if (beanActShopDetail.getStatus() == 1) {

                                mList.clear();
                                mList.addAll(beanActShopDetail.getData().getGoods());
                                mAdapter.notifyDataSetChanged();

                                Glide.with(ShopDetailActivity.this).
                                        load(beanActShopDetail.getData().getStore().getLogo())
                                        .placeholder(R.drawable.loading_pic)
                                        .error(R.drawable.error_pic)
                                        .into(mLogo);

                                tvShopName.setText(beanActShopDetail.getData().getStore().getStorename());

                                tvGoodsCount.setText(beanActShopDetail.getData().getStore().getCount_goods());
                                tvFocusCount.setText(beanActShopDetail.getData().getStore().getCount_collect());
                                tvDealCount.setText(beanActShopDetail.getData().getStore().getCount_order());

                                shopTEL = beanActShopDetail.getData().getStore().getMobile();
                            } else {
                                Toast.makeText(ShopDetailActivity.this, beanActShopDetail.getInfo(), Toast.LENGTH_SHORT).show();
                            }


                        }
                    });


        }


    }

    private void initEvent() {

        mSRL.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        GridLayoutManager mgr = new GridLayoutManager(this, 2);
        mRecyclerView.setLayoutManager(mgr);
        //设置适配器
        mAdapter = new ActShopDetailRvAdapter(R.layout.item_fraghome_bottom_gridview, mList, ShopDetailActivity.this);
        mAdapter.setHeaderView(getRvHeader());
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                // TODO: 2016/12/10 item 点击事件
                String ID = mList.get(position).getId();
                String title = mList.get(position).getTitle();
                String price = mList.get(position).getPrice();

                GoodDetailActivity.toAct(ShopDetailActivity.this, ID, title, price);

            }
        });

//        mAdapter = new ActShopDetailGvAdapter(this, mList);
//        mGridView.setAdapter(mAdapter);
//        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//                // TODO: 2016/12/10 item 点击事件
//                String ID = mList.get(position).getId();
//                String title = mList.get(position).getTitle();
//                String price = mList.get(position).getPrice();
//
//                GoodDetailActivity.toAct(ShopDetailActivity.this, ID, title, price);
//
//
//            }
//        });

        btnAllKinds.setOnClickListener(this);
        btnCallService.setOnClickListener(this);
        btnBack.setOnClickListener(this);
        llGoodsCount.setOnClickListener(this);
    }

    private void initData() {
        mList = new ArrayList<>();
    }

    private void initView() {

        btnBack = (LinearLayout) findViewById(R.id.act_shop_detail_btn_back);

        mSRL = (SwipeRefreshLayout) findViewById(R.id.act_shop_detail_srl);
        mSRL.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));

//        mLogo = (ImageView) findViewById(R.id.act_shop_detail_iv_logo);
//        tvShopName = (TextView) findViewById(R.id.act_shop_detail_tv_shopName);
//
//        llGoodsCount = (LinearLayout) findViewById(R.id.act_shop_detail_ll_goodsCount);
//        llFocusCount = (LinearLayout) findViewById(R.id.act_shop_detail_ll_focusCount);
//        llDealCount = (LinearLayout) findViewById(R.id.act_shop_detail_ll_dealCount);
//
//        tvGoodsCount = (TextView) findViewById(R.id.act_shop_detail_tv_goodsCount);
//        tvFocusCount = (TextView) findViewById(R.id.act_shop_detail_tv_focusCount);
//        tvDealCount = (TextView) findViewById(R.id.act_shop_detail_tv_dealCount);

//        mGridView = (FixedGridView) findViewById(R.id.act_shop_detail_gv);

        mRecyclerView = (RecyclerView) findViewById(R.id.act_shop_detail_rv);

        btnAllKinds = (Button) findViewById(R.id.act_shop_detail_btn_classify);
        btnCallService = (Button) findViewById(R.id.act_shop_detail_btn_call_service);
    }

    private View getRvHeader() {

        View header = LayoutInflater.from(this).inflate(R.layout.activiy_shop_detail_header, null);


        mLogo = (ImageView) header.findViewById(R.id.act_shop_detail_iv_logo);
        tvShopName = (TextView) header.findViewById(R.id.act_shop_detail_tv_shopName);

        llGoodsCount = (LinearLayout) header.findViewById(R.id.act_shop_detail_ll_goodsCount);
        llFocusCount = (LinearLayout) header.findViewById(R.id.act_shop_detail_ll_focusCount);
        llDealCount = (LinearLayout) header.findViewById(R.id.act_shop_detail_ll_dealCount);

        tvGoodsCount = (TextView) header.findViewById(R.id.act_shop_detail_tv_goodsCount);
        tvFocusCount = (TextView) header.findViewById(R.id.act_shop_detail_tv_focusCount);
        tvDealCount = (TextView) header.findViewById(R.id.act_shop_detail_tv_dealCount);

        return header;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.act_shop_detail_btn_back:
                // TODO: 2016/12/10 返回键
                onBackPressed();
                break;

            case R.id.act_shop_detail_ll_goodsCount:
                // TODO: 2016/12/10 全部宝贝

                ShopSearchResultActivity.toAct(this, shopId, null);

                break;

            case R.id.act_shop_detail_btn_classify:
                // TODO: 2016/12/10 查看全部分类
                ShopListActivity.toAct(this, shopId);

                break;
            case R.id.act_shop_detail_btn_call_service:
                // TODO: 2016/12/10 联系客服


                if (TextUtils.isEmpty(shopTEL)) {
                    return;
                }

                Log.e("shopTEL: ", shopTEL);

                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + shopTEL);
                intent.setData(data);
                startActivity(intent);

                break;

        }

    }
}
